package com.fractal;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Random;

public class Test
{
    public static void main (String... args) throws Exception
    {
	Random r = new Random();
	double x0 = r.nextDouble();
	double y0 = r.nextDouble();
	double psize = r.nextDouble();

	String url = "http://www.kilza.dk/smap/";
	String agent = "Mozilla/4.0";
	String type = "application/x-www-form-urlencoded";
	String data = 
	    URLEncoder.encode("command", "UTF-8") + "=" + URLEncoder.encode("save", "UTF-8") + "&" +
	    URLEncoder.encode("x0", "UTF-8") + "=" + URLEncoder.encode(""+x0, "UTF-8") + "&" +
	    URLEncoder.encode("y0", "UTF-8") + "=" + URLEncoder.encode(""+y0, "UTF-8") + "&" +
	    URLEncoder.encode("psize", "UTF-8") + "=" + URLEncoder.encode(""+psize, "UTF-8"); 
	
	URLConnection conn = (new URL(url)).openConnection();
	conn.setDoOutput(true);

	OutputStreamWriter wr = new OutputStreamWriter( conn.getOutputStream() );
	wr.write(data);
	wr.flush();

	BufferedReader rd =  new BufferedReader ( new InputStreamReader ( conn.getInputStream()));
	String str;

	while ( (str = rd.readLine()) != null)
	    System.out.println(str);

	wr.close();
	rd.close();
	
	// -----
	
	
	URLConnection conn2 = (new URL(url)).openConnection();
	BufferedReader in = new BufferedReader (new InputStreamReader (conn2.getInputStream()));
	
	String str2;
	System.out.println("get result:");
	while ((str2 = in.readLine()) != null)
	    System.out.println(str2);

	in.close();
	
    }
}